package com.wemei.auto.api.core.service

import java.util.*


object RouterServiceBootstrap {

    val SERVICE_MAP = mutableMapOf<Class<*>, Any>()

    /**
     * 注册公共服务
     */
    fun register(service: Any) {
        var interfaces = service::class.java.interfaces
        if (interfaces.isNotEmpty()) {
            val queue: Queue<Class<*>> = LinkedList()
            queue.addAll(interfaces)
            while (queue.isNotEmpty()) {
                val clz: Class<*> = queue.poll()!!
                SERVICE_MAP[clz] = service
                interfaces = clz.interfaces
                if (interfaces.isNotEmpty()) {
                    queue.addAll(interfaces)
                }
            }
        }
        SERVICE_MAP[service::class.java] = service
    }

    inline fun <reified T> getService(): T = SERVICE_MAP[T::class.java] as T
}
